package com.platform.comm.common.batch.listener;

import lombok.extern.log4j.Log4j2;
import org.springframework.batch.core.ChunkListener;
import org.springframework.batch.core.scope.context.ChunkContext;

import javax.annotation.Nonnull;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;

@Log4j2
public class CustomChunkListener implements ChunkListener {

    private LocalDateTime beginTime;

    @Override
    public void beforeChunk(@Nonnull ChunkContext context) {
        beginTime = LocalDateTime.now();
    }

    @Override
    public void afterChunk(@Nonnull ChunkContext context) {
        LocalDateTime endTime = LocalDateTime.now();
        long readCount = context.getStepContext().getStepExecution().getReadCount();
        long writeCount = context.getStepContext().getStepExecution().getWriteCount();
        String jobName = String.valueOf(context.getStepContext().getJobParameters().get("jobName"));
        log.warn(jobName + "：已经读取数据=" + readCount);
        log.warn(jobName + "：已经写入数据=" + writeCount);
        log.warn(jobName +"chunk耗时="+ChronoUnit.SECONDS.between(beginTime, endTime));
    }

    @Override
    public void afterChunkError(@Nonnull ChunkContext context) {

    }
}
